package com.ysstech.finance.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysstech.common.entity.PageBean;
import com.ysstech.common.enums.YssStatusEnum;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.finance.entity.VoucherTemplate;
import com.ysstech.finance.mapper.VoucherTemplateMapper;
import com.ysstech.finance.service.VoucherTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-03-13
 */
@Service
@Slf4j
public class VoucherTemplateServiceImpl extends ServiceImpl<VoucherTemplateMapper, VoucherTemplate> implements VoucherTemplateService {

    @Autowired
    private VoucherTemplateMapper voucherTemplateMapper;

    /**
     * 查询科目模板列表信息
     *
     * @param voucherTemplate
     * @param pageBean
     * @return
     * @throws Exception
     */
    public PageBean<VoucherTemplate> queryAllVoucherTemplate(VoucherTemplate voucherTemplate, PageBean pageBean) throws Exception {
        if (StringUtils.isNotBlank(voucherTemplate.getFtype())) {
            List<String> list = JSONArray.parseArray(voucherTemplate.getFtype(), String.class);
            if (!CollectionUtils.isEmpty(list) && StringUtils.isNotBlank(list.get(0))) {
                voucherTemplate.setListType(list);
            }
        }
        if (StringUtils.isNotBlank(voucherTemplate.getSource())) {
            List<String> list = JSONArray.parseArray(voucherTemplate.getSource(), String.class);
            if (!CollectionUtils.isEmpty(list) && StringUtils.isNotBlank(list.get(0))) {
                voucherTemplate.setSource(list.get(0));
            } else {
                voucherTemplate.setSource(null);
            }
        } else {
            voucherTemplate.setSource(null);
        }
        IPage pageVoucherTemplate = voucherTemplateMapper.queryVoucherTemplate(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), voucherTemplate);
        this.switchedListSubject(pageVoucherTemplate.getRecords());
        return new PageBean<>(pageVoucherTemplate);
    }

    /**
     * 类型，机构等中文的转换
     *
     * @param list
     * @return
     * @throws Exception
     */
    public void switchedListSubject(List<VoucherTemplate> list) throws Exception {
        for (VoucherTemplate voucherTemplate : list) {
            if (null == voucherTemplate) {
                continue;
            }
        }
    }

    /**
     * 新增凭证模板信息
     *
     * @param voucherTemplate 凭证模板信息
     * @param ids             核算项明细信息编码
     * @throws Exception
     */
    public void addVoucherTemplate(VoucherTemplate voucherTemplate, String ids) throws Exception {
        voucherTemplate.setId(DateUtil.generateUUID());
        voucherTemplate.setAddTime(LocalDateTime.now());
        voucherTemplate.setStatus(YssStatusEnum.YSS_NORMAL.getCode());
        this.save(voucherTemplate);
    }

    /**
     * 修改凭证模板信息
     *
     * @param voucherTemplate 凭证模板信息
     * @param ids             核算项明细信息编码
     * @throws Exception
     */
    public void editVoucherTemplate(VoucherTemplate voucherTemplate, String ids) throws Exception {
        if (voucherTemplate == null || StringUtils.isBlank(voucherTemplate.getId())) {
            throw new BusinessException("获取凭证模板信息失败，请稍后重试！");
        }
        voucherTemplate.setEditTime(LocalDateTime.now());
        this.updateById(voucherTemplate);

    }


    /**
     * 查询科目模板列表信息
     *
     * @param voucherTemplate
     * @return
     * @throws Exception
     */
    public List<VoucherTemplate> queryVoucherTemplate(VoucherTemplate voucherTemplate) throws Exception {
        return voucherTemplateMapper.queryVoucherTemplate(voucherTemplate);
    }

}
